﻿using LibraryManagement.Custom_Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LibraryManagement.Membership
{
    public partial class MemberAdd : libForm
    {
        private LibManagementEntities context;
        private List<Year> listYear;
        public enum AccountState { Active = 1, Frozen = 2, Closed = 3 }
        AccountState eAccountState;
        public enum Redirection { Add = 1, Edit = 2 }
        public Redirection eRedirection;
        private int Id;
        private MemberDetail OMemberAdd;
        public MemberAdd(Redirection zRedirection)
        {
            InitializeComponent();
            this.eRedirection = zRedirection;
        }
        public MemberAdd(Int32 id, Redirection zRedirection)
        {
            InitializeComponent();
            this.eRedirection = zRedirection;
            this.Id = id;
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (eRedirection == Redirection.Add)
                OMemberAdd = new MemberDetail();
            else if (eRedirection == Redirection.Edit)
                OMemberAdd = context.MemberDetails.Single(x => x.Id == Id);
            OMemberAdd.MemberEnrollmentNo = txtMemberId.Text;
            OMemberAdd.Name = txtName.Text;
            OMemberAdd.Email = txtEmail.Text;
            OMemberAdd.CategoryId = (decimal)cmbCategory.SelectedValue;
            OMemberAdd.Branch = Convert.ToInt16(cmbBranch.SelectedValue);
            if (cmbYear.Enabled == true)
                OMemberAdd.Year = Convert.ToInt16(cmbYear.SelectedValue);
            OMemberAdd.ContactNo = (mtbContact1.Text == "") ? Convert.ToDecimal(null) : Convert.ToDecimal(mtbContact1.Text);
            OMemberAdd.ContactNo2 = (mtbContact2.Text == "") ? Convert.ToDecimal(null) : Convert.ToDecimal(mtbContact2.Text);
            OMemberAdd.Address = rtbAddress.Text;
            OMemberAdd.AccOpeningDate = DateTime.Today.Date;
            if (rdbActive.Checked)
                eAccountState = AccountState.Active;
            else if (rdbFrozen.Checked)
                eAccountState = AccountState.Frozen;
            else if (rdbClosed.Checked)
                eAccountState = AccountState.Closed;
            OMemberAdd.AccountState = eAccountState.ToString();
            OMemberAdd.Image = clsMethods.ConvertImageToByteArray(pcbMemberImage.Image, ImageFormat.Jpeg);
            if (eRedirection == Redirection.Add)
            {
                context.MemberDetails.Add(OMemberAdd);
            }
            Random rnd = new Random();
            while (true)
            {
                var pin = rnd.Next(1000, 9999);
                if (!context.MemberDetails.Select(x => x.PinCode).ToList().Contains(pin.GetHashCode().ToString()))
                {
                    OMemberAdd.PinCode = pin.GetHashCode().ToString();
                    break;
                }
            }
            context.SaveChanges();
            this.Close();
        }

        private void MemberAdd_Load(object sender, EventArgs e)
        {
            context = new LibManagementEntities();
            SetComboBoxes();
            if (eRedirection == Redirection.Edit)
            {
                OMemberAdd = context.MemberDetails.Single(x => x.Id == Id);
                txtMemberId.Text = OMemberAdd.MemberEnrollmentNo;
                txtEmail.Text = OMemberAdd.Email;
                txtName.Text = OMemberAdd.Name;
                if (OMemberAdd.CategoryId != null)
                    cmbCategory.SelectedValue = OMemberAdd.CategoryId;
                cmbBranch.SelectedValue = OMemberAdd.Branch;
                cmbYear.SelectedValue = OMemberAdd.Year;
                mtbContact1.Text = OMemberAdd.ContactNo.ToString();
                mtbContact2.Text = OMemberAdd.ContactNo2.ToString();
                rtbAddress.Text = OMemberAdd.Address;
                if (AccountState.Active.ToString() == OMemberAdd.AccountState)
                    rdbActive.Checked = true;
                if (AccountState.Frozen.ToString() == OMemberAdd.AccountState)
                    rdbFrozen.Checked = true;
                if (AccountState.Closed.ToString() == OMemberAdd.AccountState)
                    rdbClosed.Checked = true;
                clsMethods.LoadImageFromBytes(OMemberAdd.Image, OMemberAdd, pcbMemberImage);
            }
        }

        private void SetComboBoxes()
        {
            cmbBranch.SetDataSource(context.Branches.ToList(), "BranchName", "BranchId");
            cmbYear.SetDataSource(context.Years.ToList(), "YearName", "YearId");
            cmbCategory.SetDataSource(context.Categories.ToList(), "categoryName", "CategoryId");

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnBrowseImage_Click(object sender, EventArgs e)
        {
            ofdImage.ShowDialog();
        }

        private void ofdImage_FileOk(object sender, CancelEventArgs e)
        {
            pcbMemberImage.Image = Image.FromFile(((System.Windows.Forms.FileDialog)(sender)).FileName);
        }

        private void cmbCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (cmbCategory.SelectedIndex != -1)
                {
                    if (context.Categories.Single(x => x.CategoryName == "Student").CategoryId == (decimal)cmbCategory.SelectedValue)
                        cmbYear.Enabled = true;
                    else
                        cmbYear.Enabled = false;
                }
            }
            catch (Exception)
            {
            }
        }

        private void mtbContact1_Enter(object sender, EventArgs e)
        {
            mtbContact1.SelectAll();
        }

    }

    class Year
    {
        public Year(string name, int digit)
        {
            yearName = name;
            yearDigit = digit;
        }
        public string yearName { get; set; }
        public int yearDigit { get; set; }
    }
}
